__author__ = 'zhenjiao.su'
# -*- coding: utf-8 -*-
from random import uniform
from time import sleep
from excelrw import readxls
from selenium.webdriver.support.ui import WebDriverWait
from Newwebdriver import Active
from Other import loading,datetimes
class list(Active):
    def __init__(self,driver):
        Active.__init__(self,driver)
        self.driver=driver
        self.elread=readxls("D:/pywebdriver/Pythonwebdriver/element.xls")
        self.searchsummit=(self.elread(1,5),self.elread(1,6))   #list页重搜按钮
        self.listoneresult=(self.elread(2,5),self.elread(2,6))  #list页搜索结果第1条记录
        self.listresult=(self.elread(8,5),self.elread(8,6))   #list页搜索结果
        self.listresultinaveprice=(self.elread(3,5),self.elread(3,6)) #结果中某数据的均价
        self.listresultinsumprice=(self.elread(4,5),self.elread(4,6)) #结果中某数据的总价
        self.listresultingoflight=(self.elread(5,5),self.elread(5,6))  #去程航空名
        self.listresultinbackflight=(self.elread(6,5),self.elread(6,6)) #回程航空名
        self.listresultinhotel=(self.elread(7,5),self.elread(7,6)) #酒店名称
        self.listresultinclick=(self.elread(9,5),self.elread(9,6)) #某结果的查看预订
        self.listloadstatus=(self.elread(10,5),self.elread(10,6))  #list页loading状态
        self.listwgotime=(self.elread(11,5),self.elread(11,6))    #往返时间
        self.listwbacktime=(self.elread(12,5),self.elread(12,6))
        self.listgohoteltime=(self.elread(13,5),self.elread(13,6))   #这里是单程时间
        self.listbackhoteltime=(self.elread(14,5),self.elread(14,6))
        self.listdgotime=(self.elread(15,5),self.elread(15,6))
    def listpageexit(self):    #是否进入list页
        try:
            WebDriverWait(self.find_element(self.searchsummit),10).until(lambda a:a)
            return True
        except:
            return False
    def searchexit(self,searchid=None):  #判断有没有搜索结果,取列表里是否至少有一条酒店数据，等待3s
        sleep(3)
        try:
            WebDriverWait(self.find_element(self.listoneresult),3).until(lambda a:a)
        except:
            return False
        else:
            searchlen=self.find_elements(self.listresult)   #获取搜索页的结果数，随机生成想要选择的结果
            if searchid ==None:
                self.searchid=int(uniform(1,len(searchlen)))
            else:
                self.searchid=searchid
            return True
    def searchresults(self):  #获取相关数据
        self.aveprice=self.find_element(self.listresultinaveprice,self.searchid).text
        self.sumprice=self.find_element(self.listresultinsumprice,self.searchid).text
        goflight=self.find_element(self.listresultingoflight,self.searchid).get_attribute("textContent")
        self.goflight=goflight.strip()
        backflight=self.find_element(self.listresultinbackflight,self.searchid).get_attribute("textContent")
        self.backflight=backflight.strip()
        self.gohotel=self.find_element(self.listresultinhotel,self.searchid).text
    def searchclick(self):   #随机选择产品提交
        self.find_element(self.listresultinclick).click()
    def searchloadingexit(self,timeout=30):   #判断是否处于loading状态中,最大时间30S,搜索完毕，则False,超出30S则“warning”
        return loading(self.find_element(self.listloadstatus),timeout=timeout)
    def updatewtime(self,goday=None,endday=None):   #自定义更改日期
        if goday !=None:
            gotime=self.find_element(self.listwgotime).get_attribute("value")
            print(gotime)
            newgotime=datetimes(gotime,goday)
            print(newgotime)
            godatejs=("document.getElementById(\"js_fromdate\").value=\"%s\""%newgotime)
            self.driver.execute_script(godatejs)
        if endday !=None:
            endtime=self.find_element(self.listwbacktime).get_attribute("value")
            newbacktime=datetimes(endtime,endday)
            print(newbacktime)
            enddatejs=("document.getElementById(\"js_todate\").value=\"%s\""%newbacktime)
            self.driver.execute_script(enddatejs)

